package com.carserver.controller;

import cn.hutool.json.JSONObject;
import com.carserver.common.Result;
import com.carserver.pojo.OrderForm;
import com.carserver.service.OrderFormDao;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order_form")
public class OrderFormController {
    @Resource
    OrderFormDao dao;

    @GetMapping("/0")
    public Result get_0() {
        List l = dao.getOrderFormByFlag0();
        return Result.success(l);
    }

    @GetMapping("/1")
    public Result get_1() {
        List l = dao.getOrderFormByFlag1();
        return Result.success(l);
    }

    @GetMapping("/2")
    public Result get_2() {
        List l = dao.getOrderFormByFlag2();
        return Result.success(l);
    }

    @GetMapping("/3")
    public Result get_3() {
        List l = dao.getOrderFormByFlag3();
        return Result.success(l);
    }

    @GetMapping("/4")
    public Result get_4() {
        List l = dao.getOrderFormByFlag4();
        return Result.success(l);
    }

    @GetMapping("/count")
    public Result get_number() {
        //懒得写数数的方法了,数据库小 随便写了
        List l1 = dao.getOrderFormByFlag1();
        List l2 = dao.getOrderFormByFlag2();
        List l3 = dao.getOrderFormByFlag3();
        List l4 = dao.getOrderFormByFlag4();
        List l0 = dao.getOrderFormByFlag0();
        JSONObject jsonObject=new JSONObject();
        jsonObject.set("n0",l0.size());
        jsonObject.set("n1",l1.size());
        jsonObject.set("n2",l2.size());
        jsonObject.set("n3",l3.size());
        jsonObject.set("n4",l4.size());
        return Result.success(jsonObject);
    }

    @PostMapping("/save")
    public Result save(@RequestBody OrderForm orderForm) {
        dao.save(orderForm);
        return Result.success("更新订单");
    }


    //订单详情,根据用户的id获取订单号
    @GetMapping("/user_id/{userId}")
    public Result getOrderByUserId(@PathVariable Integer userId) {
        List<OrderForm> l = dao.getOrderFormsByUserId(userId);
        return Result.success(l);
    }

    @GetMapping("/order_id/{orderId}")
    public Result getOrderByOrderId(@PathVariable String orderId) {
        OrderForm o = dao.getOrderFormsByOrderId(orderId);
        return Result.success(o);
    }

    @GetMapping("/count/evaluate")
    public Result get_evaluate(){
       Integer n5= dao.count5();
        Integer n4=dao.count4();
         Integer n3=dao.count3();
         Integer [] a=new Integer[]{n3,n4,n5};
        return Result.success(a);
    }

    @GetMapping("/have_pay/{orderID}")
    public Result have_pay1(@PathVariable  String orderID){
            OrderForm o=dao.getOrderFormsByOrderId(orderID);
            o.setHavePay("1");
            dao.save(o);
        return Result.success(o);
    }
}
